package;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.Shape;
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.Responder;
import flash.Lib;
import sandy.core.scenegraph.Sprite3D;
typedef PlayerData =
{
	var id : Int;
	var name : String;
	var x : Float;
	var y : Float;
	var z : Float;
	var forward :Float;
	var pan : Float;
	var rotation : Float;
	var health : Float;
}
class Player extends Sprite
{
	public var data:PlayerData;
	public var s3 : Sprite3D;
	private var controller : PlayerControl;
	public var fireEnabled:Bool;
	var info3D:Dynamic;
	var control:Bool;
	public function new(data:PlayerData, control:Bool, info3D)
	{
		super();
		this.data=data;
		name = data.name;
		this.control=control;
		this.info3D = info3D;
		createView3D();
		setData(data);
		if(control)
			controller = new PlayerControl(this);
		fireEnabled=true;
	}
	public function cleanup()
	{
		controller.cleanup();
		controller = null;
		data = null;
	}
	function createView3D()
	{
		s3 = new Sprite3D(data.name, new Avion(), 1);
		s3.x = data.x;
		s3.y = data.y;
		s3.z = data.z;
		//s3.moveForward(data.forward);
		//s3.pan = data.pan;
	}
	public function setData(d:PlayerData)
	{
		data = d;
		/*s3.x = data.x;
		s3.y = data.y;
		s3.z = data.z;
		*/
		s3.moveForward(data.forward);
		s3.pan = data.pan;
		/*data.x = s3.x;
		data.y = s3.y;
		data.z = s3.z;
		*/
	}
}
class PlayerControl
{
	var maxSpeed:Float;
	var rotationSpeed:Float;
	var thrust:Float;
	var decay:Float;
	var speed:Float;
	var up:Bool;
	var down:Bool;
	var left:Bool;
	var right:Bool;
	var player : Player;

	public function new(p:Player)
	{
		player = p;
		maxSpeed = 5;
		rotationSpeed = 1;
		thrust = .5;
		decay = .97;
		speed = 0;
		setEnabled(true);
	}
	public function cleanup()
	{
		setEnabled(false);
		player=null;
	}
	public function setEnabled(value:Bool):Void
	{
		if(value)
		{
			Lib.current.stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyEvent);
			Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyEvent);
		}
		else
		{
			Lib.current.stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			Lib.current.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyEvent);
			Lib.current.stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyEvent);
		}
	}
	function onKeyEvent(event:KeyboardEvent):Void
	{
		var value = event.type == KeyboardEvent.KEY_DOWN;
		switch( event.keyCode )
		{
			case Keyboard.UP: up = value;
			case Keyboard.DOWN: down = value;
			case Keyboard.LEFT: left = value;
			case Keyboard.RIGHT: right = value;
			case Keyboard.SPACE: if(player.fireEnabled){player.fireEnabled=false; fire();};
		}
	}
	private function enterFrameHandler(event:Event):Void
	{
		if(right)
			player.data.pan += rotationSpeed%360;
		if(left)
			player.data.pan -= rotationSpeed%360;
		if(up)
			player.data.forward -= thrust;
		else if(down)
			player.data.forward += thrust;
		else
			player.data.forward *= decay;
			
		if(player.data.forward > maxSpeed)
			player.data.forward = maxSpeed;
		if(player.data.forward < -maxSpeed)
			player.data.forward = -maxSpeed;

		if(left || right || up || down || Math.abs(player.data.forward) > 0.05)
			player.dispatchEvent(new Event("move"));
	}
	function fire()
	{
		player.dispatchEvent(new Event("fire"));
	}
}